package com.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.bean.AccountBean;

public class FenYe {
	private DBSource db = new SimpleDBSource();
	//每页显示5条数据
		private Integer pageSize = 5;
		
		//获取总数量
		public Integer getTotalNumber(){
			Connection conn = null;
			PreparedStatement stat = null;
			ResultSet res = null;
			Integer totalNumber = null;
			String sql = "select count(*) as c from account";
			try {
				conn = db.getConn();
				stat = conn.prepareStatement(sql);
				res = stat.executeQuery();
				while(res.next()){
					totalNumber = res.getInt("c");
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally{
				db.closeConn(conn);
			}
			return totalNumber; 
		}
		//获取总页数
		public Integer getTotalPage(){
			Integer totalNumber = getTotalNumber();
			int totalPage = (totalNumber / pageSize) + 1;
			return totalPage;
		}
		//获取当前页的数据
		//查询所有
		public List<AccountBean> getListByPage(String page){
			Connection conn = null;
			PreparedStatement stat = null;
			ResultSet res = null;
			List<AccountBean> list = new ArrayList<AccountBean>();
			int startPage = (Integer.parseInt(page)-1) * pageSize;
			String sql = "select * from account limit "+startPage+","+pageSize;
			try {
				conn = db.getConn();
				stat = conn.prepareStatement(sql);
				res = stat.executeQuery();
				while(res.next()){
					AccountBean bean = new AccountBean();
					bean.setAid(res.getInt("aid"));
					bean.setBank(res.getString("bank"));
					bean.setMoney(res.getFloat("money"));
					bean.setNo(res.getString("no"));
					bean.setSdate(res.getString("sdate"));
					bean.setUser(res.getString("user"));
					list.add(bean);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally{
				db.closeConn(conn);
			}
			return list;
		}
		
}
